home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_056 / conpackets / conpackets.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  175 lines

  1. /* ConPackets.c -  C. Scheppner, A. Finkel, P. Lindsay  CBM
  2.  *   DOS packet example
  3.  *   Requires 1.2
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <exec/types.h>
  8. #include <exec/memory.h>
  9. #include <libraries/dos.h>
  10. #include <libraries/dosextens.h>
  11. #include <devices/conunit.h>
  12.  
  13. #define ACTION_SCREEN_MODE  994L
  14. #define DOSTRUE  -1L
  15. #define DOSFALSE  0L
  16.  
  17. /* Used for checking version */
  18. ULONG DosBase;
  19.  
  20. /* Globals initialized by findWindow() */
  21. struct Window  *conWindow;
  22. struct ConUnit *conUnit;
  23.  
  24. main()
  25.    {
  26.    LONG  infile;
  27.    WORD  curX, curY;
  28.  
  29.    if(!(DosBase=(OpenLibrary("dos.library",33))))
  30.       cleanexit("\nVersion 1.2 required\n");
  31.  
  32.    if((infile = Input()) <= 0)
  33.       cleanexit("\nNo stdin ... Did you RUN this ???\n");
  34.  
  35.    if (! findWindow()) cleanexit("\nNot enough free memory\n");
  36.  
  37.    printf("\033[0 p\014");  /* Turn off cursor, home and clear */
  38.    printf("Window = $%lx   ConUnit = $%lx\n\n",conWindow, conUnit);
  39.    printf("CURSOR LIMITS:  XMax = %ld   YMax = %ld\n",
  40.               conUnit->cu_XMax + 1, conUnit->cu_YMax + 1);
  41.    curX = conUnit->cu_XCCP;
  42.    curY = conUnit->cu_YCCP;
  43.    printf("*<--- here cursor was at position %ld,%ld\n",curX,curY);
  44.  
  45.    /* Move to first position of last line and clear to EOL */
  46.    clearLast();
  47.    printf("ABSOLUTE CURSOR POSITIONING...");
  48.    Delay(100);
  49.    clearLast();
  50.    setRawCon(DOSTRUE);
  51.    printf("RAW MODE: Press ANY key...");
  52.    printf(" Hex value = %02lx",(UBYTE)getchar());
  53.    /* Maybe they pressed a string key - if so, get rest */
  54.    while(WaitForChar(infile,100L)) printf(" %02lx",(UBYTE)getchar());
  55.  
  56.    setRawCon(DOSFALSE);
  57.    printf("\nShort demo --- That's it\n");
  58.    cleanup();
  59.    }
  60.  
  61.  
  62. clearLast()
  63.    {
  64.    printf("\033[%ld;%0H\033[M", conUnit->cu_YMax + 1);
  65.    }
  66.  
  67.  
  68. cleanexit(s)
  69. char *s;
  70.    {
  71.    if(*s)    printf(s);    /* Print error */
  72.    cleanup();
  73.    exit(0);
  74.    }
  75.  
  76. cleanup()
  77.    {
  78.    setRawCon(DOSFALSE);
  79.    printf("\033[1 p\n"); /* Turn cursor on */
  80.    if(DosBase) CloseLibrary(DosBase);
  81.    }
  82.  
  83.  
  84.  
  85. /* sendpkt code - A. Finkel, P. Lindsay, C. Scheppner  CBM */
  86.  
  87. LONG setRawCon(toggle)
  88. LONG toggle;     /* DOSTRUE (-1L)  or  DOSFALSE (0L) */
  89.    {
  90.    struct MsgPort *conid;
  91.    struct Process *me;
  92.    LONG myargs[8] ,nargs, res1;
  93.  
  94.    me = (struct Process *) FindTask(NULL);
  95.    conid = (struct MsgPort *) me->pr_ConsoleTask;
  96.  
  97.    myargs[0]=toggle;
  98.    nargs = 1;
  99.    res1 = (LONG)sendpkt(conid,ACTION_SCREEN_MODE,myargs,nargs);
  100.    return(res1);
  101.    }
  102.  
  103.  
  104. LONG findWindow() /* inits conWindow and conUnit (global vars) */
  105.    {
  106.    struct InfoData *id;
  107.    struct MsgPort  *conid;
  108.    struct Process  *me;
  109.    LONG myargs[8] ,nargs, res1;
  110.  
  111.    /* Alloc to insure longword alignment */
  112.    id = (struct InfoData *)AllocMem(sizeof(struct InfoData),
  113.                                        MEMF_PUBLIC|MEMF_CLEAR);
  114.    if(! id) return(0);
  115.    me = (struct Process *) FindTask(NULL);
  116.    conid = (struct MsgPort *) me->pr_ConsoleTask;
  117.  
  118.    myargs[0]=((ULONG)id) >> 2;
  119.    nargs = 1;
  120.    res1 = (LONG)sendpkt(conid,ACTION_DISK_INFO,myargs,nargs);
  121.    conWindow = (struct Window *)id->id_VolumeNode;
  122.    conUnit = (struct ConUnit *)
  123.                  ((struct IOStdReq *)id->id_InUse)->io_Unit;
  124.    FreeMem(id,sizeof(struct InfoData));
  125.    return(res1);
  126.    }
  127.  
  128.  
  129. LONG sendpkt(pid,action,args,nargs)
  130. struct MsgPort *pid;  /* process indentifier ... (handlers message port ) */
  131. LONG action,          /* packet type ... (what you want handler to do )   */
  132.      args[],          /* a pointer to a argument list */
  133.      nargs;           /* number of arguments in list  */
  134.    {
  135.    struct MsgPort        *replyport;
  136.    struct StandardPacket *packet;
  137.  
  138.    LONG  count, *pargs, res1;
  139.  
  140.    replyport = (struct MsgPort *) CreatePort(NULL,0);
  141.    if(!replyport) return(NULL);
  142.  
  143.    packet = (struct StandardPacket *) 
  144.       AllocMem((long)sizeof(struct StandardPacket),MEMF_PUBLIC|MEMF_CLEAR);
  145.    if(!packet) 
  146.       {
  147.       DeletePort(replyport);
  148.       return(NULL);
  149.       }
  150.  
  151.    packet->sp_Msg.mn_Node.ln_Name = (char *)&(packet->sp_Pkt);
  152.    packet->sp_Pkt.dp_Link         = &(packet->sp_Msg);
  153.    packet->sp_Pkt.dp_Port         = replyport;
  154.    packet->sp_Pkt.dp_Type         = action;
  155.  
  156.    /* copy the args into the packet */
  157.    pargs = &(packet->sp_Pkt.dp_Arg1);       /* address of first argument */
  158.    for(count=0;count < nargs;count++) 
  159.       pargs[count]=args[count];
  160.  
  161.    PutMsg(pid,packet); /* send packet */
  162.  
  163.    WaitPort(replyport);
  164.    GetMsg(replyport); 
  165.  
  166.    res1 = packet->sp_Pkt.dp_Res1;
  167.  
  168.    FreeMem(packet,(long)sizeof(struct StandardPacket));
  169.    DeletePort(replyport); 
  170.  
  171.    return(res1);
  172.    }
  173.  
  174.  
  175.